<?php
class Getpwd_email_model extends BZ_Model
{
    protected $table = 'getpwd_email';

    /**
     * 查找邮箱是否存在
     * @param $params
     * @return mixed
     */
    public function findByUserName($params)
    {
        $this->db->select('id');
        $this->db->from($this->table);
        $this->db->where('username', $params['username']);
        $this->db->where('email', $params['email']);
        $this->db->limit(1);
        return $this->db->get();
    }

    /**
     * 验证码 是否存在
     * @param $userName
     * @return mixed
     */
    public function verifyTheExistence($userName, $code)
    {
        $now = date('Y-m-d H:i:s');
        $this->db->select('id, email, cdkey');
        $this->db->from($this->table);
        $this->db->where('username', $userName);
        $this->db->where('cdkey', $code);
        $this->db->where('addtime <', $now );
        $this->db->where('failtime >', $now);
        $this->db->where('flag', 0);
        $this->db->limit(1);
        return $this->db->get();
    }


    public function updateFlag($userName)
    {
        $this->db->where('username', $userName);
        return $this->db->update($this->table, array('flag' => 1, 'failtime' => date('Y-m-d H:i:s')));
    }

    public function updateCode($params)
    {
        $data = array(
            'cdkey'    => $params['cdkey'],
            'addtime'  => date('Y-m-d H:i:s'),
            'failtime' => date('Y-m-d H:i:s', strtotime('+1 days')),
            'flag'     => 0
        );
        $this->db->where('username', $params['username']);
        $this->db->where('email', $params['email']);
        return $this->db->update($this->table, $data);
    }

    public function insertInto($postData)
    {
        $data = array(
            'username' => $postData['username'],
            'email'    => $postData['email'],
            'cdkey'    => $postData['cdkey'],
            'addtime'  => date('Y-m-d H:i:s'),
            'failtime' => date('Y-m-d H:i:s', strtotime('+1 days')),
            'flag'     => 0
        );
        return $this->db->insert($this->table, $data);
    }
}